package com.xunta.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.xunta.common.utils.DataResult;
import com.xunta.entity.UserEntity;
import com.xunta.service.UserService;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;



/**
 *
 *
 * @author 不愿透露asd
 * @email *****@mail.com
 * @date 2021-08-18 15:13:11
 */
@Controller
@RequestMapping("/")
public class UserController {
    @Resource
    private UserService userService;


    /**
    * 跳转到页面
    */
    @GetMapping("/index/user")
    public String user() {
        return "user/list";
        }

    @ApiOperation(value = "新增")
    @PostMapping("user/add")
    @RequiresPermissions("user:add")
    @ResponseBody
    public DataResult add(@RequestBody UserEntity user){
        userService.save(user);
        return DataResult.success();
    }

    @ApiOperation(value = "删除")
    @DeleteMapping("user/delete")
    @RequiresPermissions("user:delete")
    @ResponseBody
    public DataResult delete(@RequestBody @ApiParam(value = "id集合") List<String> ids){
        userService.removeByIds(ids);
        return DataResult.success();
    }

    @ApiOperation(value = "更新")
    @PutMapping("user/update")
    @RequiresPermissions("user:update")
    @ResponseBody
    public DataResult update(@RequestBody UserEntity user){
        userService.updateById(user);
        return DataResult.success();
    }

    @ApiOperation(value = "查询分页数据")
    @PostMapping("user/listByPage")
    @RequiresPermissions("user:list")
    @ResponseBody
    public DataResult findListByPage(@RequestBody UserEntity user){
        Page page = new Page(user.getPage(), user.getLimit());
        LambdaQueryWrapper<UserEntity> queryWrapper = Wrappers.lambdaQuery();
        //查询条件示例
        //queryWrapper.eq(UserEntity::getId, user.getId());
        IPage<UserEntity> iPage = userService.page(page, queryWrapper);
        return DataResult.success(iPage);
    }

}
